<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '商品添加' : '商品修改'"
      :visible.sync="info.isshow"
      width="50%"
    >
      <el-form ref="form" :model="form" label-width="80px">
        <div>form:{{ form }}</div>
        <el-form-item label="活动名称">
          <el-input v-model="form.title"></el-input>
        </el-form-item>
        <!-- 日期 -->

        <el-form-item label="活动期限">
          <el-date-picker
            v-model="form.value1"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="一级分类">
          <el-select v-model="form.first_cateid" placeholder="请选择">
            <el-option label="区域一" value="shanghai"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="二级分类">
          <el-select v-model="form.second_cateid" placeholder="请选择">
            <el-option label="区域一" value="shanghai"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品">
          <el-select v-model="form.goodsid" placeholder="请选择">
            <el-option label="区域一" value="shanghai"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="状态">
          <el-switch
            v-model="form.status"
            :active-value="1"
            :inactive-value="2"
          ></el-switch>
        </el-form-item>
      </el-form>

      <el-button type="primary" @click="Add" v-if="info.isAdd">添加</el-button>
      <el-button type="primary" @click="Edit" v-else>修改</el-button>
      <el-button type="info" @click="cal">取消</el-button>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import { reqseckadd, reqseckinfo, reqseckedit } from "../../../http/api";
import { successAlert } from "../../../utils/alert";
import path from "path";
export default {
  props: ["info"],

  data() {
    return {
      form: {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
        value1: [],
      },
      shortcuts: [
        {
          text: "最近一周",
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit("pick", [start, end]);
          },
        },
        {
          text: "最近一个月",
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit("pick", [start, end]);
          },
        },
        {
          text: "最近三个月",
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit("pick", [start, end]);
          },
        },
      ],
    };
  },

  computed: {
    ...mapGetters({
      list: "seckill/list",
    }),
  },
  mounted() {},
  methods: {
    ...mapActions({
      reqList: "seckill/reqList",
    }),
    //点了添加
    Add() {
      this.form.begintime = new Date(this.form.value1[0]).getTime();
      this.form.endtime = new Date(this.form.value1[1]).getTime();
      reqseckadd(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.empty();
          this.info.isshow = false;
          this.reqList();
        }
      });
    },
    /* 点击了编修改*/
    Edit() {
      reqseckedit(this.form).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.empty();
          this.info.isshow = false;
          this.reqList();
        }
      });
    },
    cal() {
      if (!this.info.isAdd) {
        this.empty();
      }
      this.info.isshow = false;
    },
    //定义一个空数组
    empty() {
      // 清空form
      this.form = {
        title: "",
        begintime: "",
        endtime: "",
        first_cateid: "",
        second_cateid: "",
        goodsid: "",
        status: 1,
        value1: [],
      };

      // 清除图片地址
      this.imgUrl = "";
    },
    getOne(id) {
      //取回数据并对其进行操作
      reqseckinfo({ id: id }).then((res) => {
        if (res.data.code == 200) {
          successAlert(res.data.msg);
          this.form = res.data.list;
          this.form.id = id;
        }
      });
    },
  },
};
</script>
<style scoped lang="less">
@import "../../../less/index.less";
</style>